var $messageId=$("#messageId").val(); // 消息id
var $msgType=$("#msgType").val(); // 已发消息的tab传1   其他传2
$(function () {
    init() //消息详情


//如果记得时间戳是毫秒级的就需要*1000 不然就错了记得转换成整型
    $(document).on("click", "#orderBackMoney", function(){
        openTab("退款记录", "/front/orderManager/user/backMoney");

        // 提交完表单，重新加载相关table列表页面
        // refreshActivePane('/front/orderManager/user/backMoney');

        setTimeout(function () {
            var iframeDom = $(window.parent.document).find('iframe[src="/front/orderManager/user/backMoney"]')[0];
            var data = {
                orderBackMoneyId:$('#orderBackMoney').attr('data-orderbackmoneyid'),
                productOrderId:$('#orderBackMoney').attr('data-productorderid'),
            }
            iframeDom && iframeDom.contentWindow.oderDetail('/order/customerOrderBackMoneyDetail',data,0);
            iframeDom && iframeDom.contentWindow.openModal();
        },500)

    });
})

// function formatDate(now) {
//     var year=now.getFullYear();
//     var month=now.getMonth()+1;
//     var date=now.getDate();
//     var hour=now.getHours();
//     var minute=now.getMinutes();
//     var second=now.getSeconds();
//     return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
// }
//onclick="deleteReply(this, '${data.data.userForumId}');"
function init() {
    $.ajax({
        url:"/message/messageDetail",
        type: 'POST',
        dataType: "json",
        data: {
            messageId:$messageId,
            msgType:$msgType

        },
        success: (function (data){
            if(data.code==='000000'){
                let result=data.data.message;
                $("#newsReadType").val(result.newsReadType);
                $("#senderId").val(result.senderId);
                $("#receiverId").val(result.receiverId);
                $("#themeId").val(result.themeId);
                $("#theme").text(result.theme);
                $("#sendTm").html(result.sendTm);
                $("#newsDetail").html(result.newsDetail);
                $("#senderName").text(result.senderName);

                if(result.newsType==5){
                    $(".newsTypeJudge").show()
                }else{
                    $(".newsTypeJudge").hide()
                }


                let htmlList='',listAll=data.data.list;
                $(".span-num").text(listAll.length)
                for(let i=0,len=listAll.length;i<len;i++){
                    if(listAll[i].accessUrl){
                        htmlList+=`<div class="list" >
                                        <div class="head"><a href="#">
                                        
                                        <img src="${listAll[i].accessUrl}" alt="">
                                        </a></div>
                                        <div class="list-info" >
                                            <input type="text"  class="hide one-reply-id">
                                            <p><a href="#">${listAll[i].senderName}：${listAll[i].newsDetail}</a></p>
                                            <div class="list-time">${listAll[i].sendTm}</div>
                                            <div class="list-action">
                                                <a href="#!" class="delete" onclick="deleteReply(this, '${listAll[i].messageId}');"><i class="mdi mdi-delete"></i>删除</a>
                                            </div>
                                        </div>
                             
                               </div>`
                    }else{
                        htmlList+=`<div class="list" >
                                        <div class="head"><a href="#">
                                        
                                        ${listAll[i].senderName.split('')[0]}
                                        </a></div>
                                        <div class="list-info" >
                                            <input type="text"  class="hide one-reply-id">
                                            <p><a href="#">${listAll[i].senderName}：${listAll[i].newsDetail}</a></p>
                                            <div class="list-time">${listAll[i].sendTm}</div>
                                            <div class="list-action">
                                                <a href="#!" class="delete" onclick="deleteReply(this, '${listAll[i].messageId}');"><i class="mdi mdi-delete"></i>删除</a>
                                            </div>
                                        </div>
                             
                               </div>`
                    }


                }


                $(".list-all").html(htmlList)
            }
        }),
        error:(function (error) {
            console.log(error,'error')
        })



    })
};
// 删除消息
function deletInit() {
    $.ajax({
        url:"/message/deleteThemeId",
        type: 'POST',
        dataType: "json",
        data: {
            themeId:$("#themeId").val()
        },
        success: function (data) {
            if(data.code==='000000'){


                $.alert({
                    title: '提示',
                    content: "成功！",
                    buttons: {
                        confirm: {
                            text: '确认',
                            btnClass: 'btn-primary',
                            action: function () {
                                var $thisTabs = parent.$('.mt-nav-bar .nav-tabs').find('li.active');
                                var navTab = parent.$multitabs._getNavTab($thisTabs);
                                closeTab(navTab);
                                delStorageAndRemove(navTab);
                            }
                        },

                    }
                })



            }else{
                $.alert('失败！')
            }
        },
        error: function (error) {
            $.alert('失败！')
        }
    })
}

//删除单条
function deleteReply(th,messageId) {


    $.alert({
        title: '提示',
        content: "确定要删除吗？",
        buttons: {
            confirm: {
                text: '确认',
                btnClass: 'btn-primary',
                action: function () {

                    $.ajax({
                        url:"/message/deleteByMessageId",
                        type: 'POST',
                        dataType: "json",
                        data: {
                            messageId
                        },
                        success: function (data) {
                            if(data.code==='000000'){
                                init()
                            }else{
                                $.alert('失败！')
                            }
                        },
                        error: function (error) {
                            $.alert('失败！')
                        }
                    })

                }
            },
            cancel: {
                text: '取消',
                action: function () {
                }
            }
        }
    })

}

// 标记重要消息

function markImportMessage() {
    $.ajax({
        url:"/message/markImportant",
        type: 'POST',
        dataType: "json",
        data: {
            messageId:$messageId
        },
        success: function (data) {
            if(data.code==='000000'){



                $.alert('成功！')



            }else{
                $.alert('失败！')
            }
        },
        error: function (error) {
            $.alert('失败！')
        }
    })
}

// input 聚焦
function focusInput() {
    $("#content").focus()
}


// 回复

function replyMessage() {

    if($("#content").val().length<=0){
        $.alert('请输入回复的内容！');
        return false;
    }
    let data={
        "senderId":$("#senderId").val(), //发送人id
        "themeId" : $("#themeId").val(), //主题id
        "theme" : $("#theme").text(),// 主题
        "newsDetail":$("#content").val(),// 回复内容
        "newsType":$("#newsType").val(),// 消息类型
        "receiverId":$("#receiverId").val(), //接收人id
        "msgType" : $msgType  // 1已发消息   其他传2
    }
    $.ajax({
        url:"/message/replyMessage",
        type: 'POST',
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify(data),
        success: function (data) {
            if(data.code==='000000'){
                $.alert('成功！');
                $("#content").val('')
                init()
            }else{
                $.alert('失败！')
            }
        },
        error: function (error) {
            $.alert('失败！')
        }
    })
}


function backBtn() {
    var $thisTabs = parent.$('.mt-nav-bar .nav-tabs').find('li.active');
    var navTab = parent.$multitabs._getNavTab($thisTabs);
    closeTab(navTab);
    // delStorageAndRemove(navTab);
}

//补充材料弹框
let formData;
let examineId;
function openSupplementaryMaterialModal(examineIds) {
    var flag=getSysInfo()
    if(flag){
        examineId=examineIds;
        $('#supplementaryMaterialModal').modal('show');
        $("#contractImg").val('');
        $("#contractPic").val('');
        formData='';
    }else{
        $.alert({
            'title':'提示',
            'content':'请联系业务员补充材料'
        })
    }

}
//补充材料
function uploadImgExchange() {
    var $fileUpLoadExchange=$("#contractImg");
    var $fileDom = $('#contractPic');
    let fileToUpLoad = $fileUpLoadExchange.prop('files')[0];
    // 功能：合同附件点取消按钮清空文件域内容
    if (!fileToUpLoad) {
        $fileDom.val('');
        return;
    }
    // 功能：限制上传文件大小为18M内
    if (fileToUpLoad && fileToUpLoad.size > 18 * 1024 * 1024) {
        promptInfo('附件最大为18M，请重新选择！', function () {
            $fileUpLoadExchange.val('');
            $fileDom.val('');
        });
        return;
    }
    var formFile = new FormData();
    formFile.append("file", fileToUpLoad);
    formData=formFile;
    /*var data = formFile;*/
    /*var imgId;
    $.ajax({
        url: '/file/uploadByOneFile',
        data: data,
        type: "POST",
        dataType: "json",
        async: false,
        cache: false,
        processData: false,
        contentType: false,
        success: function (data) {
            if (data.code == "000000") {
                $fileDom.val(data.data.fileId)
                imgId = data.data.fileId;
            }
        },
        error: (function (res) {
            $.alert(res);
        })
    });
    return imgId;*/
}
//提交材料
function submitEvidence(){
    var file=$("#contractImg").val();
    if(!file){
        $.alert({
            'title':'提示',
            'content':'请选择要补充的材料'
        })
        return;
    }
    formData.delete("examineId");
    formData.append('examineId',examineId);
    $.ajax({
        url: '/returnMoneyExamine/uploadMaterial',
        type: 'POST',
        dataType: 'json',
        contentType: false,
        data: formData,
        processData: false,
        success:(function(result){
            $.alert({
                'title':'提示',
                'content':result.message
            })
            if(result.code=='000000'){
                $('#supplementaryMaterialModal').modal('hide');
            }
        }),
        error:(function(result){
            $.alert({
                'title':'提示',
                'content':result.message
            })
        })
    })

}
/**
 * 提示信息
 * @param content  提示内容
 */
function promptInfo(content) {
    $.alert({
        title: '温馨提示',
        content: content,
        buttons: {
            confirm: {
                text: '确认',
                btnClass: 'btn-primary'
            }
        }
    });
}
//获得登录信息
function getSysInfo(){
    var flag=true;
    $.ajax({
        url:'/sysCompany/currentInfo',
        method:'GET',
        async:false,
        success:(function(result){
            if(result.code=='000000'){
                var data=result.data;
                if(data.deptId=='3'||data.deptId=='4'||data.deptId=='6'){
                    flag=false;
                }
            }else{
                $.alert({
                    'title':'提示',
                    'content':result.message
                })
            }

        }),
        error:(function(result){
            $.alert({
                'title':'提示',
                'content':result.message
            })
        })
    })
    return flag;
}